<?php
/**
 * 商家服务 - RPC
 * 
 * @author karl <karl.tang@aiyuangong.com>
 */
namespace app\seller\rpc;

use think\Validate;

class Store extends \app\common\rpc\Rpc
{

    /**
     * 添加商户分类绑定
     * 注意：调用此方法只能是单一业务逻辑的功能，其它复杂业务，请单开接口实现
     *
     * @return bool
     */
    public function store_edit($where, $data)
    {
        $rule = [
            "name|店铺名称" => "max:50",
            "label|店铺LOGO" => "max:255",
            "avatar|店铺LOGO" => "max:255",
            "qq|QQ号" => "number|max:15",
            "phone|店铺电话" => "max:20",
            "keywords|SEO关键字" => "max:50",
            "description|SEO店铺描述" => "max:120",
            "intro|店铺简介" => "max:65535"
        ];
        $msg = ['intro.max'=>'店铺简介过长'];
        $data = filter_param($rule, $data); // 过滤字段
        $validate = new Validate($rule, $msg);
        if (! $validate->check($data)) {
            // 验证不通过
            return [
                'result' => - 1,
                'msg' => $validate->getError()
            ];
        }
        try {
            $result = \think\Db::name('store')->where($where)->update($data);
            return [
                'result' => $result
            ];
        } catch (Exception $ex) {
            // TO DO exception
            return [
                'result' => false
            ];
        }
    }

    /**
     * 商铺幻灯片更新
     * 注意：调用此方法只能是单一业务逻辑的功能，其它复杂业务，请单开接口实现
     *
     * @return bool
     */
    public function store_slide_edit($where, $data)
    {
        $rule = [
            "slide|幻灯片" => "max:500",
            "slide_url|幻灯片" => "max:500"
        ];
        $data = filter_param($rule, $data); // 过滤字段
        $validate = new Validate($rule);
        if (! $validate->check($data)) {
            // 验证不通过
            return [
                'result' => - 1,
                'msg' => $validate->getError()
            ];
        }
        try {
            $result = \think\Db::name('store')->where($where)->update($data);
            $store_info = \think\Db::name('store')->where($where)->find();
            if (empty($store_info)) {
                return [
                    'result' => - 1,
                    'msg' => '没有商铺信息'
                ];
            }
            // 删除upload表中数据
            $where = [
                'item_id' => $store_info['id'],
                'type' => 3
            ];
            $result = \think\Db::name('upload')->where($where)->delete();
            return [
                'result' => 1
            ];
        } catch (Exception $ex) {
            // TO DO exception
            return [
                'result' => false
            ];
        }
    }

    /**
     * 添加/编辑商户分类绑定
     * 注意：调用此方法只能是单一业务逻辑的功能，其它复杂业务，请单开接口实现
     *
     * @return bool
     */
    public function bind_class_add($data)
    {
        $result = \think\Db::name('store_bind_class')->where('store_id', $data['store_id'])
            ->where('class_2', $data['class_2'])
            ->find();
        if (count($result) > 0) {
            return [
                'result' => - 1,
                'msg' => '该类目已存在'
            ];
        }
        $rule = [
            "class_1|一级分类" => "number",
            "class_2|二级分类" => "number",
            "commis_rate|佣金比例" => "number|max:100",
            "store_id|店铺ID" => "number",
            "state|状态" => "number"
        ];
        $data = filter_param($rule, $data); // 过滤字段
        $validate = new Validate($rule);
        if (! $validate->check($data)) {
            // 验证不通过
            return [
                'result' => - 1,
                'msg' => $validate->getError()
            ];
        }
        try {
            $data = \think\Db::name('store_bind_class')->insert($data);
            return [
                'result' => $data
            ];
        } catch (Exception $ex) {
            // TO DO exception
            return [
                'result' => false
            ];
        }
    }

    /**
     * 添加店铺商品分类绑定
     * 注意：调用此方法只能是单一业务逻辑的功能，其它复杂业务，请单开接口实现
     *
     * @return bool
     */
    public function store_goods_class_add($data)
    {
        $rule = [
            "name|分类名称" => "max:50",
            "parent_id|上级分类" => "number",
            "store_id|店铺ID" => "number",
            "sort|排序" => "number",
            "state|状态" => "number"
        ];
        $data = filter_param($rule, $data); // 过滤字段
        $validate = new Validate($rule);
        if (! $validate->check($data)) {
            // 验证不通过
            return [
                'result' => - 1,
                'msg' => $validate->getError()
            ];
        }
        try {
            $data = \think\Db::name('store_goods_class')->insert($data);
            return [
                'result' => $data
            ];
        } catch (Exception $ex) {
            // TO DO exception
            return [
                'result' => false
            ];
        }
    }

    /**
     * 编辑店铺商品分类绑定
     * 注意：调用此方法只能是单一业务逻辑的功能，其它复杂业务，请单开接口实现
     *
     * @return bool
     */
    public function store_goods_class_edit($where, $data)
    {
        $rule = [
            "name|分类名称" => "max:50",
            "parent_id|上级分类" => "number",
            "store_id|店铺ID" => "number",
            "sort|排序" => "number",
            "state|状态" => "number"
        ];
        $data = filter_param($rule, $data); // 过滤字段
        $validate = new Validate($rule);
        if (! $validate->check($data)) {
            // 验证不通过
            return [
                'result' => - 1,
                'msg' => $validate->getError()
            ];
        }
        try {
            $result = \think\Db::name('store_goods_class')->where($where)->update($data);
            return [
                'result' => $result
            ];
        } catch (Exception $ex) {
            // TO DO exception
            return [
                'result' => false
            ];
        }
    }

    /**
     * 删除店铺商品分类绑定
     * 注意：调用此方法只能是单一业务逻辑的功能，其它复杂业务，请单开接口实现
     *
     * @return bool
     */
    public function store_goods_class_del($ids)
    {
        $class = \think\Db::name('store_goods_class')->where([
            'id' => [
                'in',
                $ids
            ]
        ])->select();
        if (! empty($class) && count($ids) > 0) {
            try {
                foreach ($class as $key => $val) {
                    $result = \think\Db::name('store_goods_class')->where([
                        'id' => $val['id']
                    ])->delete();
                    // 若是顶级分类，则需删除子分类
                    if ($val['parent_id'] == 0) {
                        $result = \think\Db::name('store_goods_class')->where([
                            'parent_id' => $val['id']
                        ])->delete();
                    }
                }
                return [
                    'result' => true
                ];
            } catch (Exception $ex) {
                // TO DO exception
                return [
                    'result' => false
                ];
            }
        }
        return [
            'result' => true
        ];
    }

    /**
     * 保存品牌
     * 注意：调用此方法只能是单一业务逻辑的功能，其它复杂业务，请单开接口实现
     *
     * @param $type 'edit'代表编辑，'add'代新增，delete代表'删除'
     * @return bool
     */
    public function brand_save($type, $where, $data)
    {
        $rule = [
            "name|品牌名称" => "max:100",
            "initial|名称首字母" => "max:1",
            "class_id|品牌类别" => "number",
            "class_name|品牌类别" => "max:50",
            "pic|品牌图标" => "max:200",
            "store_id|店铺ID" => "number",
            "apply|排序" => "number",
            "isdelete|删除" => "number"
        ];
        $data = filter_param($rule, $data); // 过滤字段
        $validate = new Validate($rule);
        if (! $validate->check($data)) {
            // 验证不通过
            return [
                'result' => - 1,
                'msg' => $validate->getError()
            ];
        }
        try {
            if ($type == 'add') {
                $data = \think\Db::name('goods_brand')->insert($data);
            } elseif ($type == 'edit') {
                $data = \think\Db::name('goods_brand')->where($where)->update($data);
            } elseif ($type == 'delete') {
                $data = \think\Db::name('goods_brand')->where($where)->update($data);
            }
            return [
                'result' => $data
            ];
        } catch (Exception $ex) {
            // TO DO exception
            return [
                'result' => false
            ];
        }
    }

    /**
     * 删除商户相关信息
     * 注意：调用此方法只能是单一业务逻辑的功能，其它复杂业务，请单开接口实现
     *
     * @return bool
     */
    public function delete($table, $where)
    {
        $result = \think\Db::name($table)->where($where)->select();
        if (count($result) == 0) {
            return [
                'result' => true
            ];
        }
        $data = \think\Db::name($table)->where($where)->delete();
        return [
            'result' => $data
        ];
    }

    /**
     * 取分类列表，按照深度归类
     *
     * @param array $condition 检索条件
     * @param int $show_deep 显示深度
     * @return array 数组类型的返回结果
     */
    public function tree_class_list($where, $show_deep = '2')
    {
        $class_list = $this->getStoreGoodsClassList($where);
        $show_deep = intval($show_deep);
        $result = array();
        if (is_array($class_list) && ! empty($class_list)) {
            $result = $this->_getTreeClassList($show_deep, $class_list);
        }
        return $result;
    }

    /**
     * 递归 整理分类
     *
     * @param int $show_deep 显示深度
     * @param array $class_list 类别内容集合
     * @param int $deep 深度
     * @param int $parent_id 父类编号
     * @param int $i 上次循环编号
     * @return array $show_class 返回数组形式的查询结果
     */
    private function _getTreeClassList($show_deep, $class_list, $deep = 1, $parent_id = 0, $i = 0)
    {
        static $show_class = array(); // 树状的平行数组
        if (is_array($class_list) && ! empty($class_list)) {
            $size = count($class_list);
            if ($i == 0)
                $show_class = array(); // 从0开始时清空数组，防止多次调用后出现重复
            for ($i; $i < $size; $i ++) { // $i为上次循环到的分类编号，避免重新从第一条开始
                $val = $class_list[$i];
                $stc_id = $val['id'];
                $stc_parent_id = $val['parent_id'];
                if ($stc_parent_id == $parent_id) {
                    $val['deep'] = $deep;
                    $show_class[] = $val;
                    if ($deep < $show_deep && $deep < 2) { // 本次深度小于显示深度时执行，避免取出的数据无用
                        $this->_getTreeClassList($show_deep, $class_list, $deep + 1, $stc_id, $i + 1);
                    }
                }
                if ($stc_parent_id > $parent_id)
                    break; // 当前分类的父编号大于本次递归的时退出循环
            }
        }
        return $show_class;
    }

    /**
     * 取分类列表
     *
     * @param array $condition 检索条件
     * @return array 数组类型的返回结果
     */
    public function class_tree($where)
    {
        $class_list = $this->getStoreGoodsClassList($where);
        $d = array();
        if (is_array($class_list)) {
            foreach ($class_list as $v) {
                if ($v['parent_id'] == 0) {
                    $d[$v['id']] = $v;
                } else {
                    if (isset($d[$v['parent_id']]))
                        $d[$v['parent_id']]['child'][] = $v; // 防止出现父类不显示时子类被调出
                }
            }
        }
        return $d;
    }

    /**
     * 类别列表
     *
     * @param array $condition 检索条件
     * @return array 数组结构的返回结果
     */
    public function getStoreGoodsClassList($where, $order = 'parent_id asc,sort asc,id asc')
    {
        $result = \think\Db::name('store_goods_class')->where($where)
            ->order($order)
            ->select();
        return $result;
    }
}
